package com.yang.guliproduct.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yang.guliproduct.entity.PmsAttrGroup;
import com.yang.guliproduct.service.impl.PmsAttrGroupServiceImpl;
import com.yang.guliproduct.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 属性分组 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-07-08
 */
@RestController
@RequestMapping("/guliproduct/pms-attr-group")
public class PmsAttrGroupController {

    @Autowired
    private PmsAttrGroupServiceImpl pmsAttrGroupService;

    @PostMapping("getAttrGroupList/{current}/{limit}")
    public R getAttrGroupList(@PathVariable Long current, @PathVariable Long limit,
                              @RequestBody(required = false) PmsAttrGroup pmsAttrGroup){
        QueryWrapper<PmsAttrGroup> queryWrapper = new QueryWrapper<>();
        Page<PmsAttrGroup> page = new Page<>(current,limit);
        Long catelogId = pmsAttrGroup.getCatelogId();
        if (!StringUtils.isEmpty(catelogId)){
            queryWrapper.eq("catelog_id",catelogId);
        }
        pmsAttrGroupService.page(page,queryWrapper);
        long total = page.getTotal();
        List<PmsAttrGroup> records = page.getRecords();
        return R.ok().data("total",total).data("records",records);
    }

    @PostMapping("addAttrGroup")
    public R addAttrGroup(@RequestBody PmsAttrGroup pmsAttrGroup){
        pmsAttrGroupService.save(pmsAttrGroup);
        return R.ok();
    }

    @PostMapping("updateAttrGroup")
    public R updateAttrGroup(@RequestBody PmsAttrGroup pmsAttrGroup){
        pmsAttrGroupService.updateById(pmsAttrGroup);
        return R.ok();
    }

    @DeleteMapping("deleteAttrGroup/{attrGroupId}")
    public R deleteAttrGroup(@PathVariable Long attrGroupId){
        pmsAttrGroupService.removeById(attrGroupId);
        return R.ok();
    }

    @GetMapping("getById/{attrGroupId}")
    public R getById(@PathVariable Long attrGroupId){
        PmsAttrGroup attrGroup = pmsAttrGroupService.getById(attrGroupId);
        return R.ok().data("attrGroup",attrGroup);
    }

}

